package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequirePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.*;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private ApplicationContext ctx;





    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public Permission get(Long id) {

        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {

        return  permissionMapper.selectAll();
    }

    @Override
    public PageResult<Permission> query(QueryObject qo) {
        int count = permissionMapper.selectCount(qo);
        if (count==0){
            return new PageResult<>
                    (qo.getCurrentPage(),qo.getPageSize(), Collections.emptyList(),count);
        }


        int totalPage=count%qo.getPageSize()==0?
                count/qo.getPageSize():count/qo.getPageSize()+1;



        if (qo.getCurrentPage()>totalPage){
            qo.setCurrentPage(totalPage);
        }

        List<Permission> permissions = permissionMapper.selectForList(qo);
        return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),permissions,count);

    }

    @Override
    public void reload() {

        //获取数据库中所有的权限对象
        List<Permission> permissions = permissionMapper.selectAll();

        //将所有的权限对象的表达式（唯一）放进一个集合中
        HashSet<String> stringHashSet = new HashSet<>();

        for (Permission permission : permissions) {
            stringHashSet.add(permission.getExpression());
        }


        Map<String, Object> beans = ctx.getBeansWithAnnotation(Controller.class);


        Collection<Object> controllers = beans.values();


        for (Object controller : controllers) {
            Class<?> aClass = controller.getClass();

            Method[] methods = aClass.getDeclaredMethods();

            for (Method method : methods) {
                RequirePermission requirePermission = method.getAnnotation(RequirePermission.class);

                if (requirePermission==null){
                    continue;
                }


                String name = requirePermission.name();

                String expression = requirePermission.expression();


                //set集合不允许重复  且添加会返回对应的boolean值
                if (stringHashSet.add(expression)){
                    Permission permission = new Permission();

                    permission.setName(name);

                    permission.setExpression(expression);


                    permissionMapper.insert(permission);
                }



            }


        }



    }

    @Override
    public List<String> selectByEmployeeId(Long employeeId) {
        return permissionMapper.selectByEmployeeId(employeeId);
    }

    @Override
    public List<Permission> selectByRoleId(Long roleId) {
        return permissionMapper.selectByRoleId(roleId);
    }


}
